<template>
  <div class="equipment-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit(null)">
          新增新资讯
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              placeholder="请输入标题"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="title"
        label="标题"
      ></el-table-column>
       <el-table-column
        show-overflow-tooltip
        prop="title"
        label="描述"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="image"
        label="图片"
      >
      <template slot-scope="scope">
        <img v-if="scope.row.image" :src="fileServerURL + scope.row.image" style="width: 50px;height: 50px;border: 1px solid #ccc">
      </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="操作"
      >
        <template #default="{ row }">
          <el-button
            v-show="row.approved != 1"
            icon="el-icon-edit"
            type="primary"
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button
            icon="el-icon-delete"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
import { getList, doAdd, doEdit,doDelete } from '@/api/info'
import { baseURL, fileServerURL} from '@/config/settings'
import Edit from './components/edit'

export default {
  name: 'InfoManagementList',
  components: {Edit},
  data() {
    return {
      list: null,
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      selectRows: '',
      elementLoadingText: '正在加载...',
      fileServerURL:'',
      queryForm: {
        pageNo: 1,
        pageSize: 10,
        title: '',
      },
    }
  },
  created() {
    this.fetchData()
    this.fileServerURL = fileServerURL
  },
  methods: {
    setSelectRows(val) {
      this.selectRows = val
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row)
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm('你确定要删除该资讯吗？', null, async () => {
          await doDelete(row.id)
          this.$baseMessage('资讯删除成功', 'success')
          this.fetchData()
        })
      }
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val
      this.fetchData()
    },
    queryData() {
      this.queryForm.pageNo = 1
      this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      const { records, totalElement } = await getList(this.queryForm)
      this.list = records
      this.total = totalElement
      setTimeout(() => {
        this.listLoading = false
      }, 300)
    },
  },
}
</script>
<style lang="scss" scoped>
.el-row {
  &:last-child {
    margin-bottom: 20px;
  }
}
.el-table__body {
  .el-link:last-child {
    margin-left: 10px;
  }
}
</style>
